package csu.rjxyg.action.userAction;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.BeanFactory;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.util.LogUtil;

/**
 * 提交参数keyword来搜索userName中包含keyword的账号信息
 * @author acer
 *
 */
public class SearchUserByNameAction extends BaseAction {
	private LogUtil log;
	
	private String keyword;
	
	public SearchUserByNameAction(){
		this.log = UtilFactory.getLogUtil(this.getClass());
	}
	
	public void searchUserByName() throws IOException{
		log.info("查找用户：关键词为：" + keyword);
		if(keyword == null || keyword.equals("")){
			this.out("error:关键词不能为空");
		}
		//得到当前用户
		BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
		//获取dao
		BaseDaoAdapter userDAO = DaoFactory.getDaoByColName(UserConstant.COLLECTION_NAME);
		//正则表达式
		Pattern pattern = Pattern.compile("^.*" + keyword + ".*$");
		//创建索引
		BaseObject temp = (BaseObject) factory.getBean(BaseBean.BEAN_OBJECT);
		temp.set(UserConstant.USER_NAME, pattern);
		//查找
//		BaseL objList = userDAO.getBeanInfoByExample(temp);
		BaseList baseList = userDAO.getBeanInfoByExample(temp);
		
		for( int i = 0 ; i < baseList.size(); i++ ){
			baseList.get(i).set(UserConstant.USER_PASS, "");
		}
		
		baseList = this.checkPermission(baseList, (String) object.get(UserConstant.USER_LOGIN_QX));
		
		//将用户权限转化为可理解的中文
		baseList = this.changePermissionName(baseList);
		
		//向前端输出结果
		this.out(baseList.getBean().toString());
		
		//将用户权限还原为系统识别
		temp = this.changePermissionName(temp);
		System.out.println(temp.getBean().toString());
		
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
}
